package java_lang_ref.weakreference;

import java.lang.ref.WeakReference;

public class WeakReferenceTest {

	public static void main(String[] argz){
		WeakReferenceTest t = new WeakReferenceTest();
		t.test();
	}

	A obj = new A(2);  
	public void test(){
		final WeakReference<A> wr = new WeakReference<A>(obj);
		
		obj = null;  
		
		//等待一段时间，obj对象就会被垃圾回收 
		new Thread(new Runnable(){
			@Override
			public void run() {
				while(true){
					if (wr.get()==null) {  
						System.out.println("obj 已经被清除了 "); 
						return;
					} else {  
						System.out.println("obj 尚未被清除，其信息是 " + wr.get().getI()); 
					}
				}				
			}			
		}).start();
	}
	public class A {
		private int i;

		public A(int j) {
			this.i = j;
		}

		public int getI() {
			return i;
		}

		public void setI(int i) {
			this.i = i;
		}
	}
}